package com.itheima.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.itheima.bean.User;
import com.itheima.service.LoginService;

public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		response.setHeader("content-type", "text/html;charset=utf-8");
		//Filter来处理请求头的中文utf-8乱码问题
		//获取请求的用户名和密码
		String username = request.getParameter("username");
		System.out.println("username="+username);
		String password = request.getParameter("password");
		System.out.println("password="+password);
		//保存登录的信息
		User saveUser=new User();
		saveUser.setUsername(username);
		saveUser.setPassword(password);
		//获取前台录入的验证码
		String yzm = request.getParameter("yzm");
		System.out.println("yzm="+yzm);
		//从session中获取图片中的验证码
		String sessionYzm=(String)request.getSession().getAttribute("sessionYzm");
		System.out.println("sessionYzm="+sessionYzm);
		//清空session   保证点击登录的时候  验证码是最新的
		request.getSession().removeAttribute("sessionYzm");
		//创建LoginService
		LoginService ls = new LoginService();
		try {
			//校验验证码
			if(yzm==""||yzm.trim().length()==0){
				//把错误信息放入request域中
				request.setAttribute("msg", "请输入验证码");
				//请求转发
				request.getRequestDispatcher("/login.jsp").forward(request, response);
				//如果程序中出现多个请求转发  那么需要在每一个请求转发后面加上return
				return;
			}else{
				//如果两个验证不一样  需要给出提示
				if(!yzm.equalsIgnoreCase(sessionYzm)){
					//把错误信息放入request域中
					request.setAttribute("msg", "请输入正确的验证码");
					//请求转发
					request.getRequestDispatcher("/login.jsp").forward(request, response);
					return;
				}
			}
			//调用service中的方法
			User user=ls.getUserByUsernameAndPwd(username,password);
			//根据返回的对象，判断提示信息的内容
			if(user==null){
				//获取前台'记住用户名'的value值
				String remember = request.getParameter("remember");
				//response.getWriter().println("登录失败");
				System.out.println("remember="+remember);
				//把错误信息放入request域对象中
				request.setAttribute("msg", "用户名和密码不匹配");
				//判断是否勾选'记住用户名'
				if("ok".equals(remember)){
					//存放用户填写的登录信息
					request.setAttribute("user",saveUser);
				}
				//使用请求转发跳转到login.jsp
				request.getRequestDispatcher("/login.jsp").forward(request, response);
				return;
				//不能使用重定向，因为使用重定向后request失效
				//response.sendRedirect("/day1102/login.jsp");
			}else{
				//获取前台'自动登录'的value值
				String flag = request.getParameter("flag");
				//判断是否勾选'自动登录'
				if("ok".equals(flag)){
					//创建cookie,把当前的用户名和密码放入cookie中（tom-123）
					Cookie cookie = new Cookie("usernameAndPwd", user.getUsername()+"-"+user.getPassword());
					//设置cookie的存活时间
					cookie.setMaxAge(3600);
					//把cookie写回浏览器
					response.addCookie(cookie);
				}
				request.getSession().setAttribute("username", user.getUsername());
//				response.getWriter().println(user.getUsername()+":欢迎回来");
				//重定向到首页
				response.sendRedirect(request.getContextPath()+"/login-index.jsp");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
